package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.uuid.UUID;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.CheckInApplyDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.utils.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 入住Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-27
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService
{
    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return save(checkIn) ? 1 : 0;
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return updateById(checkIn) ? 1 : 0;
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private CheckInMapper checkInMapper;

    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    @Transactional
    @Override
    public void apply(CheckInApplyDto checkInApplyDto) {
      //1.判断老人是否入住
        LambdaQueryWrapper<Elder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Elder::getIdCardNo,checkInApplyDto.getCheckInElderDto().getIdCardNo())
                    .eq(Elder::getStatus,1);
        Elder elder = elderMapper.selectOne(queryWrapper);
        //1.1 如果以入住-----报错已入住
        if(elder!=null){
           throw new BaseException("老人已入住,不能重新申请");
        }
      //2.更新床位状态为已入住-----我要把老人申请的房间的床状态变更已入住
        Bed bed = new Bed();
        bed.setBedStatus(1);
        bed.setId(checkInApplyDto.getCheckInConfigDto().getBedId());
        bed.setRoomId(checkInApplyDto.getCheckInConfigDto().getRoomId());
        bedMapper.updateById(bed);

      //3.新增或更新老人的信息
        Elder elder1 = insertOrUpdate(checkInApplyDto);

      //4.新增合同办理
        Contract contract = new Contract();
        BeanUtil.copyProperties(checkInApplyDto.getCheckInContractDto(),contract);
        contract.setStatus(0);
        contract.setContractNumber(CodeGenerator.generateContractNumber());
        contract.setStartDate(checkInApplyDto.getCheckInConfigDto().getStartDate());
        contract.setEndDate(checkInApplyDto.getCheckInConfigDto().getEndDate());
        contract.setElderId(elder1.getId());
        contract.setElderName(elder1.getName());
        contractMapper.insertContract(contract);

      //5.新增入住信息
        CheckIn checkIn = new CheckIn();
        checkIn.setBedNumber(elder1.getBedNumber());
        checkIn.setElderId(elder1.getId());
        checkIn.setElderName(elder1.getName());
        checkIn.setStartDate(checkInApplyDto.getCheckInConfigDto().getStartDate());
        checkIn.setEndDate(checkInApplyDto.getCheckInConfigDto().getEndDate());
        checkIn.setIdCardNo(elder1.getIdCardNo());
        checkIn.setNursingLevelName(checkInApplyDto.getCheckInConfigDto().getNursingLevelName());
        checkIn.setRemark(JSONUtil.toJsonStr(checkInApplyDto.getElderFamilyDtoList()));
        checkIn.setStatus(0);
        checkIn.setSortOrder(0);
        checkInMapper.insertCheckIn(checkIn);

      //6.新增入住配置信息
        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtil.copyProperties(checkInApplyDto.getCheckInConfigDto(),checkInConfig);
        checkInConfig.setCheckInId(checkIn.getId());
        checkInConfigMapper.insertCheckInConfig(checkInConfig);

    }

    private Elder insertOrUpdate(CheckInApplyDto checkInApplyDto) {
        Elder elder = new Elder();
        BeanUtil.copyProperties(checkInApplyDto.getCheckInElderDto(),elder);
        elder.setStatus(1);
        elder.setBedId(checkInApplyDto.getCheckInConfigDto().getBedId());
        Bed bed = bedMapper.selectBedById(elder.getBedId());
        elder.setBedNumber(bed.getBedNumber());
        elder.setCreateTime(new Date());

        //查询判断---老人有没有
        LambdaQueryWrapper<Elder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Elder::getIdCardNo,elder.getIdCardNo())
                .eq(Elder::getStatus,1);

        Elder older = elderMapper.selectOne(queryWrapper);
        if(older ==null){
            elderMapper.insertElder(elder);
        }else {
            elderMapper.updateById(elder);
        }
        return elder;
    }
}
